<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		
		
	}

    function fromp()
    {
        if(isset($_POST['login_uri']))
		{
			$u = $_POST['login_uri'];
            $this->session->set_userdata('divert_after_login', $u);
		}
        else
        {
            $this->session->unset_userdata('divert_after_login', '');
        }
        echo site_url('login');
    }
    
	function from($page_to_divert_to = FALSE)
	{
		if(!($page_to_divert_to===FALSE))
		{
			$this->session->set_userdata('divert_after_login', $page_to_divert_to);
		}
        else
        {
            $this->session->unset_userdata('divert_after_login', '');
        }
		redirect('login');
	}


	function index($page_to_divert_to = FALSE)
	{
		if(!($page_to_divert_to===FALSE))
		{
			$this->session->set_userdata('divert_after_login', $page_to_divert_to);
		}	
		
		$this->ShowHeader();
		$this->load->view('login_page');
		$this->ShowFooter();
	}
	
	function dologin()
	{
		$res = $this->users->load_by_username($_POST['username']);
		
		if($res === FALSE)
		{
			$this->session->unset_userdata('username');
			$this->session->unset_userdata('userinfo');
			$this->session->set_flashdata('loginfls', '<p style="color: #882222">Invalid username or password.</p>');
			redirect('login');
		}
	
		$this->session->set_userdata('username', $_POST['username']);
		$this->session->set_userdata('userinfo', $res);
		
		$dest =  $this->session->userdata('divert_after_login');	
		if($dest === FALSE)
		{
			$dest = 'news';
		}
		else
		{
			$dest = urldecode($dest);
		}
		
		$this->session->unset_userdata('divert_after_login');
		redirect($dest);
	}	
	
	function dologout()
	{
		$this->session->unset_userdata('username');
		$this->session->unset_userdata('userinfo');
		
		redirect('news');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */